package android.coocoo.common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.xmlpull.v1.XmlSerializer;

import android.coocoo.models.CategoryInfo;
import android.coocoo.models.Settings;
import android.coocoo.models.TaskInfo;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class XmlParser {
	
	private static String settingsFilePath;
	private static String tasksListFilePath;
	private static String categoriesListFilePath;
	private static ArrayList<TaskInfo> tasksList;
	private static ArrayList<CategoryInfo> categoriesList;
	private static Settings settings;
	
	public XmlParser(){
		XmlParser.settingsFilePath = "";
		XmlParser.tasksListFilePath = "";
		XmlParser.categoriesListFilePath = "";
		readFromXml();
	}
	
	public String getSettingsFilePath(){
		return XmlParser.settingsFilePath;
	}
	
	public void setSettingsFilePath(String settingsFilePath){
		XmlParser.settingsFilePath = settingsFilePath;
	}
	
	public String getTasksListFilePath(){
		return XmlParser.tasksListFilePath;
	}
	
	public void setTasksListFilePath(String tasksListFilePath){
		XmlParser.tasksListFilePath = tasksListFilePath;
	}
	
	public String getCategoriesListFilePath(){
		return XmlParser.categoriesListFilePath;
	}
	
	public void setCategoriesListFilePath(String categoriesListFilePath){
		XmlParser.categoriesListFilePath = categoriesListFilePath;
	}
	
	public void setTasksList(ArrayList<TaskInfo> tasksList) {
		XmlParser.tasksList = tasksList;
	}

	public ArrayList<TaskInfo> getTasksList() {
		return tasksList;
	}
	
	public void setSettings(Settings settings) {
		XmlParser.settings = settings;
		File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml");
        try{
                newxmlfile.createNewFile();
        }catch(IOException e){
                Log.e("IOException", "exception in createNewFile() method");
        }
        //we have to bind the new file with a FileOutputStream
        FileOutputStream fileos = null;        
        try{
                fileos = new FileOutputStream(newxmlfile);
        }catch(FileNotFoundException e){
                Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
        //we create a XmlSerializer in order to write xml data
        XmlSerializer serializer = Xml.newSerializer();
        try {
        	//we set the FileOutputStream as output for the serializer, using UTF-8 encoding
        	serializer.setOutput(fileos, "UTF-8");
        	//Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
        	serializer.startDocument(null, Boolean.valueOf(true));
        	//set indentation option
	        	serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
	        	//start a tag called "root"
	        	serializer.startTag(null, "settings");
	        		//i indent code just to have a view similar to xml-tree
	        		serializer.startTag(null, "mute");
	        		//write some text inside <child3>
	        		if (XmlParser.settings.isMute())
	        			serializer.text("1");
	        		else serializer.text("0");
	        		serializer.endTag(null, "mute");
	                                
	        		serializer.startTag(null, "turn-off");
	        		//write some text inside <child3>
	        		if (XmlParser.settings.isMute())
	        			serializer.text("1");
	        		else serializer.text("0");
	        		serializer.endTag(null, "turn-off");
	                               
	        	serializer.endTag(null, "settings");
        	serializer.endDocument();
        	
        	//write xml data into the FileOutputStream
        	serializer.flush();
        	//finally we close the file stream
        	fileos.close();
                       
        } catch (Exception e) {
        	Log.e("Exception","error occurred while creating xml file");
        }
    }

	public Settings getSettings() {
		return settings;
	}
	
	public void setCategoriesList(ArrayList<CategoryInfo> categoriesList) {
		XmlParser.categoriesList = categoriesList;
	}

	public ArrayList<CategoryInfo> getCategoriesList() {
		return categoriesList;
	}
	
	public boolean writeToXml(){
		return true;
	}
	
	public void readFromXml(){
		ArrayList<TaskInfo> tasksList = new ArrayList<TaskInfo>();
		TaskInfo taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,21,23,59));
		taskInfo.setTitle("1");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,21,23,59));
		taskInfo.setTitle("2");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,22,23,59));
		taskInfo.setTitle("3");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,23,23,59));
		taskInfo.setTitle("4");
		taskInfo.setCategoryId(1);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,21,23,59));
		taskInfo.setTitle("5");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,25,23,59));
		taskInfo.setTitle("6");
		taskInfo.setCategoryId(2);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,21,23,59));
		taskInfo.setTitle("7");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,29,21,30));
		taskInfo.setTitle("8");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,29,21,35));
		taskInfo.setTitle("9");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		taskInfo = new TaskInfo();
		taskInfo.setStartTime(new Date(2011,Calendar.DECEMBER,29,21,35));
		taskInfo.setTitle("10");
		taskInfo.setCategoryId(0);
		tasksList.add(taskInfo);
		XmlParser.tasksList = tasksList;
		
		Settings settings = new Settings();
		XmlParser.settings = settings;
		
		ArrayList<CategoryInfo> categoriesList = new ArrayList<CategoryInfo>();
		CategoryInfo category = new CategoryInfo();
		categoriesList.add(category);
		category = new CategoryInfo();
		category.setTitle("Category 1");
		categoriesList.add(category);
		category = new CategoryInfo();
		category.setTitle("Category 2");
		categoriesList.add(category);
		category = new CategoryInfo();
		category.setTitle("Category 3");
		categoriesList.add(category);
		XmlParser.categoriesList = categoriesList;
	}


}
